<?php
//gradeAssignment.php
//performs the grading action on the specified assignment, for the specified user.
$allow=true;
require_once("overhead.php");
//Get the ID from the url
$assignmentID = (isset($_GET['aid']) ? $_GET['aid'] : 0);
$msg="";
$score=0;
//check to make sure the user has a submission for the selected assignment.
$checkForSubmissionQuery=mysql_query("select ID as latestSubmission from submission where UserID='".$currentUser."' and AssignmentID=".$assignmentID." order by Date desc");
$row=mysql_fetch_assoc($checkForSubmissionQuery);
if ($row['latestSubmission']!= null) {
	//unzip the user's submission file.
	//print "unzip assignments/assignment_".$assignmentID."/submissions/".escapeshellcmd($currentUser)."/".$currentUser."_".$assignmentID.".zip -d assignments/assignment_".$assignmentID."/submissions/".$currentUser."/";
		exec("unzip assignments/assignment_".$assignmentID."/submissions/".$currentUser."/".$currentUser."_".$assignmentID.".zip -d assignments/assignment_".$assignmentID."/submissions/".$currentUser."");
	//unzip the instructor's test file.
		exec("unzip assignments/assignment_".$assignmentID."/copy/t.zip -d assignments/assignment_".$assignmentID."/instructorTests/");
	//copy test cases into directory.

	//run test.
	exec("java -jar Driver.jar /Users/ryan/Sites/senior/assignments/assignment_".$assignmentID."/submissions > test.txt");
	//insert test results into db.
	$insertScoreQuery=mysql_query("update submission set Score=".$score." where ID=".$row['latestSubmission']);
	if ($insertScoreQuery) { $msg="Success! :D"; }
} else {
	$msg="You have not submitted anything for this assignment!";
}


//redirect to assignment information page w/ status message.
header("Location: viewAssignment.php?aid=".$assignmentID."&msg=".$msg);
exit;
?>